﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <!-- Documenting T:NAnt.Contrib.Tasks.TryCatchTask-->
  <head>
    <meta http-equiv="Content-Language" content="en-ca" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="../style.css" />
    <title>&lt;trycatch&gt; Task</title>
  </head>
  <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="2" class="NavBar">
      <tr>
        <td class="NavBar-Cell">
          <a href="">
            <b>NAntContrib</b>
          </a>
          <img alt="-&gt;" src="../images/arrow.gif" />
          <a href="../index.html">Help</a>
          <img alt="-&gt;" src="../images/arrow.gif" />
          <a href="../tasks/index.html">Task Reference</a>
          <img alt="-&gt;" src="../images/arrow.gif" /> &lt;trycatch&gt;</td>
        <td class="NavBar-Cell" align="right">
                        v0.85</td>
      </tr>
    </table>
    <h1>&lt;trycatch&gt;</h1>
    <p> Executes a set of tasks, and optionally catches a build exception to allow recovery or rollback steps to be taken, or to define some steps to be taken regardless if the tasks succeed or fail, or both. </p>
    <p> The tasks defined in the <code>&lt;<code>try</code>&gt;</code> block will be executed in turn, as they normally would in a target. </p>
    <p> If a <code>&lt;<code>catch</code>&gt;</code> block is defined, the tasks in that block will be executed in turn only if one of the tasks in the <code>&lt;<code>try</code>&gt;</code> block fails. This failure will then be suppressed by the <code>&lt;<code>catch</code>&gt;</code> block. </p>
    <p> The message associated with the failure can also be caught in a property for use within the <code>&lt;<code>catch</code>&gt;</code> block. The original contents of the property will be restored upon exiting the <code>&lt;<code>catch</code>&gt;</code> block. </p>
    <p> If a <code>&lt;<code>finally</code>&gt;</code> block is defined, the tasks in that block will be executed after the tasks in both the <code>&lt;<code>try</code>&gt;</code> and <code>&lt;<code>catch</code>&gt;</code> blocks have been executed, regardless of whether any task fails in either block. </p>
    <h3>Parameters</h3>
    <div class="table">
      <table>
        <tr>
          <th>Attribute</th>
          <th style="text-align: center;">Type</th>
          <th>Description</th>
          <th style="text-align: center;">Required</th>
        </tr>
        <tr>
          <td valign="top">failonerror</td>
          <td style="text-align: center;">bool</td>
          <td> Determines if task failure stops the build, or is just reported. The default is <b>true</b>. </td>
          <td style="text-align: center;">False</td>
        </tr>
        <tr>
          <td valign="top">if</td>
          <td style="text-align: center;">bool</td>
          <td> If <b>true</b> then the task will be executed; otherwise, skipped. The default is <b>true</b>. </td>
          <td style="text-align: center;">False</td>
        </tr>
        <tr>
          <td valign="top">unless</td>
          <td style="text-align: center;">bool</td>
          <td> Opposite of <code>if</code>. If <b>false</b> then the task will be executed; otherwise, skipped. The default is <b>false</b>. </td>
          <td style="text-align: center;">False</td>
        </tr>
        <tr>
          <td valign="top">verbose</td>
          <td style="text-align: center;">bool</td>
          <td> Determines whether the task should report detailed build log messages. The default is <b>false</b>. </td>
          <td style="text-align: center;">False</td>
        </tr>
      </table>
    </div>
    <h3>Nested Elements:</h3>
    <!--Element-->
    <h4>
      <a id="try">
      </a>
                    &lt;try&gt;
                </h4>
    <div class="nested-element"> The tasks in this block will be executed as a normal part of the build script. <p> Executes embedded tasks in the order in which they are defined. </p><h3>Parameters</h3><div class="table"><table><tr><th>Attribute</th><th style="text-align: center;">Type</th><th>Description</th><th style="text-align: center;">Required</th></tr><tr><td valign="top">failonerror</td><td style="text-align: center;">bool</td><td> Determines if task failure stops the build, or is just reported. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">if</td><td style="text-align: center;">bool</td><td> If <b>true</b> then the task will be executed; otherwise, skipped. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">unless</td><td style="text-align: center;">bool</td><td> Opposite of <code>if</code>. If <b>false</b> then the task will be executed; otherwise, skipped. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">verbose</td><td style="text-align: center;">bool</td><td> Determines whether the task should report detailed build log messages. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr></table></div><p /></div>
    <h4>
      <a id="try">
      </a>
                    &lt;/try&gt;
                </h4>
    <!--Element-->
    <h4>
      <a id="catch">
      </a>
                    &lt;catch&gt;
                </h4>
    <div class="nested-element"> The tasks in this block will be executed if any task in the try block fails. <p></p><h3>Parameters</h3><div class="table"><table><tr><th>Attribute</th><th style="text-align: center;">Type</th><th>Description</th><th style="text-align: center;">Required</th></tr><tr><td valign="top">property</td><td style="text-align: center;">string</td><td> Defines the name of the property to save the message describing the failure that has been caught. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">failonerror</td><td style="text-align: center;">bool</td><td> Determines if task failure stops the build, or is just reported. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">if</td><td style="text-align: center;">bool</td><td> If <b>true</b> then the task will be executed; otherwise, skipped. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">unless</td><td style="text-align: center;">bool</td><td> Opposite of <code>if</code>. If <b>false</b> then the task will be executed; otherwise, skipped. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">verbose</td><td style="text-align: center;">bool</td><td> Determines whether the task should report detailed build log messages. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr></table></div><p /></div>
    <h4>
      <a id="catch">
      </a>
                    &lt;/catch&gt;
                </h4>
    <!--Element-->
    <h4>
      <a id="finally">
      </a>
                    &lt;finally&gt;
                </h4>
    <div class="nested-element"> The tasks in this block will always be executed, regardless of what happens in the try and catch blocks. <p> Executes embedded tasks in the order in which they are defined. </p><h3>Parameters</h3><div class="table"><table><tr><th>Attribute</th><th style="text-align: center;">Type</th><th>Description</th><th style="text-align: center;">Required</th></tr><tr><td valign="top">failonerror</td><td style="text-align: center;">bool</td><td> Determines if task failure stops the build, or is just reported. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">if</td><td style="text-align: center;">bool</td><td> If <b>true</b> then the task will be executed; otherwise, skipped. The default is <b>true</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">unless</td><td style="text-align: center;">bool</td><td> Opposite of <code>if</code>. If <b>false</b> then the task will be executed; otherwise, skipped. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr><tr><td valign="top">verbose</td><td style="text-align: center;">bool</td><td> Determines whether the task should report detailed build log messages. The default is <b>false</b>. </td><td style="text-align: center;">False</td></tr></table></div><p /></div>
    <h4>
      <a id="finally">
      </a>
                    &lt;/finally&gt;
                </h4>
    <h3>Examples</h3>
    <ul class="examples">
      <li>
        <pre class="code">
&lt;trycatch&gt;
    &lt;try&gt;
        &lt;echo message="In try" /&gt;
        &lt;fail message="Failing!" /&gt;
    &lt;/try&gt;
    &lt;catch&gt;
        &lt;echo message="In catch" /&gt;
    &lt;/catch&gt;
    &lt;finally&gt;
        &lt;echo message="Finally done" /&gt;
    &lt;/finally&gt;
&lt;/trycatch&gt;
    </pre>
        <p> The output of this example will be: </p>
        <pre class="code">In try
In catch
Finally done
  </pre>
        <p> The failure in the <code>&lt;<code>try</code>&gt;</code> block will not cause the build to fail. </p>
      </li>
      <li>
        <pre class="code">
&lt;trycatch&gt;
    &lt;try&gt;
        &lt;echo message="In try" /&gt;
        &lt;fail message="Just because..." /&gt;
    &lt;/try&gt;
    &lt;catch property="failure"&gt;
        &lt;echo message="Caught failure: ${failure}" /&gt;
        &lt;fail message="Bad catch" /&gt;
    &lt;/catch&gt;
    &lt;finally&gt;
        &lt;echo message="Finally done" /&gt;
    &lt;/finally&gt;
&lt;/trycatch&gt;
    </pre>
        <p> The output of this example will be: </p>
        <pre class="code">In try
Caught failure: Just because...
Finally done
Build failed: Bad catch
  </pre>
        <p> Like the above, the failure in the <code>&lt;<code>try</code>&gt;</code> block does not cause the build to fail. The failure in the <code>&lt;<code>catch</code>&gt;</code> block does, however. Note that the <code>&lt;<code>finally</code>&gt;</code> block is executed even though the <code>&lt;<code>catch</code>&gt;</code> block failed. </p>
      </li>
      <li>
        <pre class="code">
&lt;trycatch&gt;
    &lt;try&gt;
        &lt;echo message="In try" /&gt;
        &lt;fail message="yet again" /&gt;
    &lt;/try&gt;
    &lt;catch property="failure"&gt;
        &lt;echo message="Caught failure ${failure}" /&gt;
        &lt;fail message="Bad catch" /&gt;
    &lt;/catch&gt;
    &lt;finally&gt;
        &lt;echo message="Finally done ${failure}" /&gt;
    &lt;/finally&gt;
&lt;/trycatch&gt;
    </pre>
        <p> The output of this example will be: </p>
        <pre class="code">In try
Caught failure yet again
Build failed: Property 'failure' has not been set.
  </pre>
        <p> The <a href="http://nant.sourceforge.net/help/tasks/echo.html">&lt;echo&gt;</a> task in the <code>&lt;<code>finally</code>&gt;</code> block failed because the "failure" property was not defined after exiting the <code>&lt;<code>catch</code>&gt;</code> block. Note that the failure in the <code>&lt;<code>finally</code>&gt;</code> block has eclipsed the failure in the <code>&lt;<code>catch</code>&gt;</code> block. </p>
      </li>
      <li>
        <pre class="code">
&lt;trycatch&gt;
    &lt;try&gt;
        &lt;property name="temp.file" value="${path::get-temp-file-name()}" /&gt;
        &lt;do-stuff to="${temp.file}" /&gt;
        &lt;fail message="Oops..." /&gt;
    &lt;/try&gt;
    &lt;finally&gt;
        &lt;echo message="Cleaning up..." /&gt;
        &lt;if test="${property::exists('temp.file')}"&gt;
            &lt;delete file="${temp.file}" /&gt;
        &lt;/if&gt;
    &lt;/finally&gt;
&lt;/trycatch&gt;
    </pre>
        <p> A more concrete example, that will always clean up the generated temporary file after it has been created. </p>
      </li>
    </ul>
    <h3>Requirements</h3>
    <div style="margin-left: 20px;">
      <b>Assembly:</b> NAnt.Contrib.Tasks (0.85.2479.0)
            </div>
  </body>
</html>